using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Money_Log_Mobile.DB;

namespace Money_Log_Mobile
{
    public class Bank
    {
        public double Balance;
        public int LastTransactionID;
        public List<Transaction> TransactionList = new List<Transaction>();
        public List<StandingOrder> StandingOrdersList = new List<StandingOrder>();
        public Bank(string Filename)
        {
            Open();
            //todo:retrieve standing orders
            
            //TODO:retrieve last day standing orders were checked
            DateTime LastStandingOrderDate = DateTime.Now;
            while(LastStandingOrderDate < DateTime.Now)
            {
                MoneyData mdata = new MoneyData();
                //DataSet ds =  mdata.DB.Config.GetData();
                foreach(StandingOrder oStandingOrder in StandingOrdersList)
                {
                    if(oStandingOrder.IsDueOn(LastStandingOrderDate))
                    {
                        AddTransaction(oStandingOrder.Name, oStandingOrder.Amount, oStandingOrder.Categories, LastStandingOrderDate);
                    }
                }
                LastStandingOrderDate.AddDays(1);
            }
            //TODO:Save last day standing orders were checked
        }
        public void UpdateBalance(double BalanceAmount, DateTime Date)
        {
            Balance = BalanceAmount;
        }

        public int AddTransaction(string Name, Double TransactionAmount, List<string> Categories, DateTime Date)
        {
            Transaction oTransaction = new Transaction();
            oTransaction.TransactionID = LastTransactionID + 1;
            LastTransactionID++;
            oTransaction.Amount = TransactionAmount;
            oTransaction.Name = Name;
            oTransaction.Categories = Categories;
            oTransaction.Date = Date;
            TransactionList.Add(oTransaction);
            Balance = Balance - oTransaction.Amount;
            return oTransaction.TransactionID;
        }
        public void  DeleteTransaction(int iTransactionID)
        {
            Transaction TransactionToDelete = new Transaction();
            foreach (Transaction oTransaction in TransactionList)
            {
                if(oTransaction.TransactionID == iTransactionID)
                {
                    TransactionToDelete = oTransaction;
                    Balance = Balance - TransactionToDelete.Amount;
                }
            }
            if(TransactionToDelete != new Transaction())
            {
                TransactionList.Remove(TransactionToDelete);
            }

        }
        public void AddStandingOrder(string Name, Double TransactionAmount, List<string> Categories, StandingOrderFrequency Frequency)
        {
            StandingOrder oStandingOrder = new StandingOrder();
            oStandingOrder.Name = Name;
            oStandingOrder.Amount = TransactionAmount;
            oStandingOrder.Categories = Categories;
            oStandingOrder.Frequency = Frequency;
            if (oStandingOrder.IsDueOn(DateTime.Now))
            {
                AddTransaction(oStandingOrder.Name, oStandingOrder.Amount, oStandingOrder.Categories, DateTime.Now);
            }
            StandingOrdersList.Add(oStandingOrder);
        }
        public void Save()
        {
            XML oXML = new XML();
            oXML.Write(Balance, TransactionList, StandingOrdersList);
        }
        private void Open()
        {
            XML oXML = new XML();
            List<string> Categories;
            oXML.Read(out TransactionList,out StandingOrdersList,out Categories ,out Balance);
            LastTransactionID = TransactionList.Count;
        }
    }
}
